package mks.webshop.gaedao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.List;

import mks.webshop.dao.DaoManager;
import mks.webshop.dao.IMasterDao;
import mks.webshop.entity.BasePanel;
import mks.webshop.entity.Category;
import mks.webshop.entity.PanelType;
import mks.webshop.entity.Product;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.datastore.Text;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class GAEMstDaoTest {
    private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());

    // MasterDao with VN language code
    IMasterDao iMstDao = DaoManager.getMasterDaoInst();

    @Before
    public void setUp() throws Exception {
        helper.setUp();
    }

    @After
    public void tearDown() throws Exception {
        helper.tearDown();
    }

    @Test
    public void testSaveCategory() {
        // 1
        Category cat = new Category();
        cat.setId("01");
        cat.setName("Ä�á»“ gia dá»¥ng");
        cat.setLevel(1);

        assertTrue(iMstDao.saveCat(cat));

        // 2
        cat = new Category();
        cat.setId("01A");
        cat.setName("Ä�á»“ báº¿p");
        cat.setLevel(2);

        assertTrue(iMstDao.saveCat(cat));

        testGetCategoryList();
        testGetSubCategoryList();
        // assertTrue(iMstDao.deleteCatByKey(cat.getKey()));
    }
    
    @Test
    public void testGetAmountSubCategory(){
    	Category cat = new Category();
    	cat.setId("S");
    	cat.setName("Sach");
    	cat.setLevel(1);
    	assertTrue(iMstDao.saveCat(cat));
    	
    	Category cat1 = new Category();
    	cat1.setId("S1");
    	cat1.setName("Sach mau");
    	cat1.setLevel(2);
    	assertTrue(iMstDao.saveCat(cat1));
    	
    	Category cat2 = new Category();
    	cat2.setId("S2");
    	cat2.setName("Sach mau1");
    	cat2.setLevel(2);
    	assertTrue(iMstDao.saveCat(cat2));
    	
    	assertEquals(2, iMstDao.getAmountSubcategory("S", 1));
    }
    
    @Test
    public void testGetParentCategory(){
    	Category cat = new Category();
    	cat.setId("S");
    	cat.setName("Sach");
    	cat.setLevel(1);
    	assertTrue(iMstDao.saveCat(cat));
    	
    	Category cat1 = new Category();
    	cat1.setId("S1");
    	cat1.setName("Sach mau");
    	cat1.setLevel(2);
    	assertTrue(iMstDao.saveCat(cat1));
    	
    	Category cat11 = new Category();
    	cat11.setId("S11");
    	cat11.setName("Sach mau_1");
    	cat11.setLevel(3);
    	assertTrue(iMstDao.saveCat(cat11));
    	
    	Category cat111 = new Category();
    	cat111.setId("S111");
    	cat111.setName("Sach mau_1_1");
    	cat111.setLevel(4);
    	assertTrue(iMstDao.saveCat(cat111));
    	
    	List<Category> testList = iMstDao.getParentCategoryList(cat111.getId(), cat111.getLevel());
    	for (Category cat2 : testList) {
            System.out.println("Test " + cat2.getName() + "; " + cat2.getLevel());
        }
    	assertEquals(4, testList.size());
    }
    
    @Test
    public void testSaveProduct() {
		// 1

		Category cat = new Category();
		cat.setId("S");
		cat.setName("Danh muc S");
		assertTrue(iMstDao.saveCat(cat));

		Product pro = new Product();
		pro.setId("S1");
		pro.setName("San pham S1");
		assertTrue(iMstDao.saveProduct(pro));

		testGetSubProductList();
		// assertTrue(iMstDao.deleteCatByKey(cat.getKey()));
	}
    
    @Test
    public void testgetSubProductList(){
    	Category cat = new Category();
    	cat.setId("S");
    	assertTrue(iMstDao.saveCat(cat));
    	
    	Product pro1 = new Product();
    	pro1.setId("S1");
    	pro1.setCatId("S");
    	assertTrue(iMstDao.saveProduct(pro1));
    	
    	Product pro2 = new Product();
    	pro2.setId("S2");
    	pro2.setCatId("S");
    	assertTrue(iMstDao.saveProduct(pro2));
    	
    	Product pro3 = new Product();
    	pro3.setId("S3");
    	pro3.setCatId("S");
    	assertTrue(iMstDao.saveProduct(pro3));
    	
    	Product pro4 = new Product();
    	pro4.setId("S4");
    	pro4.setCatId("S");
    	assertTrue(iMstDao.saveProduct(pro4));
    	
    	 List<Product> proList = iMstDao.getSubProductList("S");
    	 assertEquals(4, proList.size());
    	
    }
    
    @Test
    public void testGetSubProductList() {
        List<Product> proList = iMstDao.getSubProductList("S");
        assertEquals(1, proList.size());

       Product protest = proList.get(0);
       assertEquals("S1", protest.getId());
       assertEquals("San pham S1", protest.getName());
    }

    @Test
    public void testGetCategoryList() {
        List<Category> catList = iMstDao.getCategoryList();

        for (Category cat : catList) {
            System.out.println("" + cat.getKey() + ";" + cat.getName() + ";" + cat.getLevel());
        }
    }

    @Test
    public void testGetSubCategoryList() {
        List<Category> catList = iMstDao.getSubCategoryList("01", 1);
        for (Category cat : catList) {
            System.out.println("" + cat.getKey() + ";" + cat.getId() + ";" + cat.getName() + ";" + cat.getLevel());
        }

        assertEquals(1, catList.size());

        // Leaf category
        catList = iMstDao.getSubCategoryList("01A", 3);
        assertEquals(catList.size(), 0);
    }
    
    

    @Test
    public void testDeleteAllCategory() {
        // 1
        Category cat1 = new Category();
        cat1.setId("01");
        cat1.setName("Ä�á»“ gia dá»¥ng");
        cat1.setLevel(1);

        assertTrue(iMstDao.saveCat(cat1));
        
        List<Category> catList = iMstDao.getCategoryList();
        for (Category cat : catList) {
            System.out.println("" + cat.getKey() + ";" + cat.getName());
            iMstDao.deleteCatByKey(cat.getKey());
        }
        
        catList = iMstDao.getCategoryList();
        assertEquals(0, catList.size());
    }

    @Test
    public void testDeleteCategoryById01() {
        // 1
        Category cat1 = new Category();
        cat1.setKey((long) 1);
        cat1.setId("01");
        cat1.setName("Ä�á»“ gia dá»¥ng");
        cat1.setLevel(1);
        assertTrue(iMstDao.saveCat(cat1));
        
        Category cat2 = new Category();
        cat1.setKey((long) 2);
        cat2.setId("012");
        cat2.setName("cat2");
        cat2.setParentId("01");
        cat2.setLevel(2);
        assertTrue(iMstDao.saveCat(cat2));
        
        Category cat3 = new Category();
        cat1.setKey((long) 3);
        cat3.setId("0123");
        cat3.setName("cat3");
        cat3.setParentId("012");
        cat3.setLevel(3);
        assertTrue(iMstDao.saveCat(cat3));
        
        Product pro1 = new Product();
        pro1.setId("pro1");
        pro1.setCatId("03");
        pro1.setName("Product 1");
        assertTrue(iMstDao.saveProduct(pro1));
        
        iMstDao.deleteCatByKey(cat1.getKey());
        List<Category> catList = iMstDao.getAllCategoryList();
        for (Category cat4:catList){
        	 System.out.println("Test " + cat4.getName() + "; " + cat4.getLevel());
        }
        Product testPro = iMstDao.getProductById("pro1");
        assertEquals(2, catList.size());
        assertEquals("Product 1", testPro.getName());
    }
    
    @Test
    public void tesetGetCategoryById() {
    	GAEMstDao dao = new GAEMstDao();
        // 1
        Category cat1 = new Category();
        cat1.setId("01");
        cat1.setName("Ä�á»“ gia dá»¥ng");
        cat1.setLevel(1);

        assertTrue(iMstDao.saveCat(cat1));
        
        // 2
        cat1 = new Category();
        cat1.setId("011");
        cat1.setName("Ä�á»“ lÃ m vÆ°á»�n");
        cat1.setLevel(1);

        assertTrue(iMstDao.saveCat(cat1));
//        Category cat = iMstDao.getCategoryById("01");
        Category cat = dao.getCategoryById("01");
        
        assertEquals(1, cat.getLevel());

        
    }
    
    @Test
    public void testSavePanel() {
        BasePanel panel = new BasePanel();
        panel.setId("footer");
        panel.setType(PanelType.FOOTER);
        panel.setContent(new Text("<b>Footer</b>"));
        
        iMstDao.savePanel(panel);
        
        // Verify by get list
        List<BasePanel> panelList = iMstDao.getAllPanels();
        assertEquals(1, panelList.size());
        
        BasePanel footerPanel = panelList.get(0);
        assertEquals(PanelType.FOOTER, footerPanel.getType());
        assertEquals("<b>Footer</b>", footerPanel.getContent().getValue());
        
        // Verify by get specified Type
        footerPanel = iMstDao.getPanel(PanelType.FOOTER);
        assertEquals(PanelType.FOOTER, footerPanel.getType());
        assertEquals("<b>Footer</b>", footerPanel.getContent().getValue());
        
        // Testing update
        footerPanel.setContent(new Text("New Footer"));
        
        // Verify by get list
        panelList = iMstDao.getAllPanels();
        assertEquals(1, panelList.size());
        
        footerPanel = panelList.get(0);
        assertEquals(PanelType.FOOTER, footerPanel.getType());
        assertEquals("New Footer", footerPanel.getContent().getValue());
        
        // Verify by get specified Type
        footerPanel = iMstDao.getPanel(PanelType.FOOTER);
        assertEquals(PanelType.FOOTER, footerPanel.getType());
        assertEquals("New Footer", footerPanel.getContent().getValue());
    } 
}
